package com.junsi.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.junsi.bean.Sources;
import com.junsi.bean.TradeManages;
import com.junsi.service.SourcesService;

import net.sf.json.JSONArray;

@Controller
@RequestMapping("/sources")
public class SourcesController {
	@Autowired
	private SourcesService sourcesService;
	Logger logger = Logger.getRootLogger();

	/**
	 * 新增
	 */
	@RequestMapping("/insert")
	public void insert(Sources sources, HttpServletResponse response) throws Exception {
		boolean result = sourcesService.insert(sources);
		Integer id = sources.getSourceId();
		Sources sources2 = null;
		if (result) {
			sources2 = sourcesService.findById(id);
		}
		JSONArray jsonArray = JSONArray.fromObject(sources2);
		response.setContentType("text/json; charset=UTF-8");
		try {
			response.getWriter().write(jsonArray.toString());
			response.getWriter().flush();
			response.getWriter().close();
		} catch (IOException e) {
			logger.error("传输失败");
			e.printStackTrace();
		}
	}

	/**
	 * 刪除
	 */
	@RequestMapping("/delete")
	public void delete(Integer id, HttpServletResponse response) {
		boolean result = sourcesService.delete(id);
		Map<String, String> map = new HashMap<String, String>();
		if (result) {
			map.put("errorMsg", "true");

		} else {
			map.put("errorMsg", "false");
			logger.error("删除失败");
		}
		JSONArray jsonArray = JSONArray.fromObject(map);
		response.setContentType("text/json; charset=UTF-8");
		try {
			response.getWriter().write(jsonArray.toString());
			response.getWriter().flush();
			response.getWriter().close();
		} catch (IOException e) {
			logger.error("传输删除失败");
			e.printStackTrace();
		}
	}

	/**
	 * 更新
	 * 
	 * @throws Exception
	 */
	@RequestMapping("/updata")
	public void updata(Sources sources, HttpServletResponse response) throws Exception {
		boolean result = sourcesService.update(sources);
		Map<String, String> map = new HashMap<String, String>();
		if (result) {
			map.put("errorMsg", "true");

		} else {
			map.put("errorMsg", "false");
			logger.error("更新失敗");
		}
		JSONArray jsonArray = JSONArray.fromObject(map);
		response.setContentType("text/json; charset=UTF-8");
		try {
			response.getWriter().write(jsonArray.toString());
			response.getWriter().flush();
			response.getWriter().close();
		} catch (IOException e) {
			logger.error("传输失败");
			e.printStackTrace();
		}
	}

	/**
	 * 查詢
	 * 
	 * @throws Exception
	 */
	@RequestMapping("/select")
	public ModelAndView select(HttpServletResponse response) throws Exception {
		ModelAndView modelAndView=new ModelAndView();
		List<Sources> list = sourcesService.listAll();
		   modelAndView.addObject("list", list);
		      modelAndView.setViewName("forward:");
		      return modelAndView;
	}
}
